package zj.com.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import zj.com.model.DjcModel;
import zj.com.model.GjgxDB;
import zj.com.util.DateUtil;
import zj.com.zj.R;

/**
 * Created by xi on 16/6/3.
 */
public class ZlzpListAdapter extends BaseAdapter{

    public List<GjgxDB> list;
    public Context context;
    public LayoutInflater inflater;
    public boolean isEdit = false;
    public Map<Integer,Boolean> selectMap = new HashMap<Integer,Boolean>();
    public int selectPosition = -1;

    public ZlzpListAdapter(List<GjgxDB> list, Context context){

        this.list = list;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = new ViewHolder();
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.adapter_dqr_sggx_item, null);
           // viewHolder.type_iv = (ImageView) convertView.findViewById(R.id.type_iv);
            viewHolder.cb = (CheckBox) convertView.findViewById(R.id.cb);
            viewHolder.name_tv = (TextView) convertView.findViewById(R.id.name_tv);
            viewHolder.date_tv = (TextView) convertView.findViewById(R.id.date_tv);
            viewHolder.date_tv.setVisibility(View.GONE);
            viewHolder.des_tv = (TextView) convertView.findViewById(R.id.des_tv);
            viewHolder.status_tv = (TextView) convertView.findViewById(R.id.status_tv);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        GjgxDB db = list.get(position);
        viewHolder.name_tv.setText(db.getGjgx());
        viewHolder.des_tv.setText(db.getProject()+"-"+db.getGs());

       int mode =  db.getMode();
        if(mode == 1){
            viewHolder.status_tv.setText("修改");

        }else{
            viewHolder.status_tv.setText("新增");

        }



        String time =db.getTime().replace("T"," ");

       // viewHolder.date_tv.setText(DateUtil.formatStrimgDate2(time));

        if(isEdit){
            viewHolder.cb.setVisibility(View.VISIBLE);
        }else{
            viewHolder.cb.setVisibility(View.GONE);

        }
        viewHolder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if(isChecked){
                    selectMap.put(position,isChecked);
                }else{
                    selectMap.remove(position);
                }
            }
        });

        if(selectPosition == position){
            selectMap.put(position,true);
            viewHolder.cb.setChecked(true);
        }else{
            selectMap.remove(position);
            viewHolder.cb.setChecked(false);
        }


        return convertView;
    }
    private static class ViewHolder {
        ImageView type_iv;
        TextView itemTitle;
        CheckBox cb;
        TextView name_tv;
        TextView date_tv;
        TextView des_tv;
        TextView status_tv;
    }


    public void setSelect(int position){

        this.selectPosition = position;
        notifyDataSetChanged();
    }

    public void setIsEdit(boolean isEdit){

        this.isEdit = isEdit;
        notifyDataSetChanged();
    }
    public void setDataChange(List<GjgxDB> list){

        this.list = list;
        notifyDataSetChanged();
    }
    public Map<Integer,Boolean> getSelectMap(){

        return selectMap;
    };
}
